package com.fly.lambda.service;

import com.fly.lambda.dao.ChannelDAO;
import com.fly.lambda.domain.Channel;
import com.fly.lambda.framework.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Fly on 2016/7/14.
 */
@Service
public class ChannelService {

    @Autowired
    ChannelDAO channelDAO;

    @Transactional
    public Channel getById(String channelId){
        Channel channel = channelDAO.getById(channelId);
        if (null == channel){
            channel = new Channel();
        }
        return channel;
    }

    @Transactional
    public void saveChannel(Channel channel){
        if (StringUtils.isBlank(channel.getChannelId())){
            //id为空，则新增
            channel.setChannelId(CommonUtils.genPK());
            channelDAO.create(channel);
        }
        else {
            channelDAO.update(channel);
        }
    }

    public void deleteById(String channelId){
        channelDAO.deleteById(channelId);
    }

    @Transactional
    public List<Channel> getList(){
        return channelDAO.getList();
    }


}
